package fr.castorhoz.isi_connectivity_widget.BluetoothWidget;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import fr.castorhoz.isi_connectivity_widget.ConnBroadcastReceiver;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;
import fr.castorhoz.isi_connectivity_widget.WifiWidget.WifiButton;

public class BluetoothBroadcastReceiver extends ConnBroadcastReceiver {

	public BluetoothBroadcastReceiver(ConnButton button) {
		super(button);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();

		if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
			Log.i(this.getClass().toString(), "bluetooth changed");

			int info = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);

			// METTRE A JOUR LES IMAGES ET LES ETATS DES COMPONENTS
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);
			int selector = button.getSelectorUnknown();

			if (info == (BluetoothAdapter.STATE_ON)) {
				Log.i(this.getClass().toString(), "Status of bluetooth : ON");
				Toast.makeText(context.getApplicationContext(),
						"Status of bluetooth : ON", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorOn();
				button.setState(ConnButton.STATE_ON);
			} else if (info == (BluetoothAdapter.STATE_TURNING_ON)) {
				Log.i(this.getClass().toString(),
						"Status of bluetooth : turning on");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : turning on", Toast.LENGTH_SHORT)
						.show();
				selector = WifiButton.selector_on;
				button.setState(ConnButton.STATE_ON);
			} else if (info == (BluetoothAdapter.STATE_OFF)) {
				Log.i(this.getClass().toString(), "Status of bluetooth : OFF");
				Toast.makeText(context.getApplicationContext(),
						"Status of bluetooth : OFF", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorOff();
				button.setState(ConnButton.STATE_OFF);
			} else if (info == (BluetoothAdapter.STATE_TURNING_OFF)) {
				Log.i(this.getClass().toString(),
						"Status of bluetooth : turning off");
				Toast.makeText(context.getApplicationContext(),
						"Status of bluetooth : turning off", Toast.LENGTH_SHORT)
						.show();
				selector = button.getSelectorOff();
			}
			//remoteViews.setImageViewResource(button.getIdButton(), selector);
			remoteViews.setImageViewResource(R.id.imageButtonWidgetRow, selector);

		}
	}

}
